import json
from typing import Dict, List
from colorama import Fore
from console_conpent.select import select
from effect.util.console.SuperConsole import print_dict_as_column_table
from effect.util.data.clear import split_dict
from entiys.npc.NPCLoader import NPCLoader


def inner(slave_infos_str):
    data = split_dict(slave_infos_str, 10)
    for i in data:
        print_dict_as_column_table(i, 34)


class ShowMySlave:
    @staticmethod
    def choose(savename, playername):
        def get_salve(chara, attribute: dict):
            return attribute.get('所属奴隶主') == playername

        slaves = NPCLoader(saveName=savename).getByMethod(get_salve)
        try:
            names = [list(info.keys())[0] for info in slaves]
        except IndexError:
            print("npc.json代码格式错误! 请检查游戏完整性")
        slave_infos: List[Dict] = []
        for i in slaves:
            slave_info: dict = list(i.values())[0]
            slave_infos.append(slave_info.items())
        allinfo = []
        for i in slave_infos:
            slave_infos_str = {}
            for name, value in i:
                if value is None:
                    value = "空"
                value = str(value)
                if value.isnumeric():
                    int_value = int(value)
                    if int_value >= 80:
                        value = Fore.GREEN + value + Fore.WHITE
                        name = Fore.GREEN + name + Fore.WHITE
                    elif 80 > int_value >= 60:
                        value = Fore.CYAN + value + Fore.WHITE
                        name = Fore.CYAN + name + Fore.WHITE
                    elif 60 > int_value > 50:
                        value = value
                    else:
                        value = Fore.RED + value + Fore.WHITE
                        name = Fore.RED + name + Fore.WHITE

                slave_infos_str[name] = [value]

            allinfo.append(lambda _slave_infos_str=slave_infos_str: inner(json.loads(json.dumps(_slave_infos_str))))

        _selec = select(*[
            "请选择你的子系统",
            ["列表 点击返回"] + names,
            [lambda: ...] + allinfo
        ], print_mode="call")
        _selec.show(color="ignore")
        return _selec
